package com.elinkway.infinitemovies.http.parser;

import android.util.Base64;

import com.elinkway.infinitemovies.bean.UpdateSnifferInfo;
import com.elinkway.infinitemovies.utils.StringUtil;

import org.json.JSONObject;

public class UpdateSnifferParser extends LVideoMobileParser<UpdateSnifferInfo> {

    private static final String CODE = "code";
    private static final String SCRIPT = "script";
    private static final String VERSION = "version";
    private static final String CREATETIME = "createtime";

    @Override
    public UpdateSnifferInfo parse(JSONObject data) throws Exception {

        UpdateSnifferInfo updateInfo = new UpdateSnifferInfo();
        String version = data.getString(VERSION);

        String code = null;
        try {
            // decode 处理不了‘+’ 号
//            String decodeStr = data.getString(CODE).replace("+", "%20");
//            code = URLDecoder.decode(decodeStr, "UTF-8");
            code = new String(Base64.decode(data.getString(SCRIPT).getBytes(), Base64.DEFAULT));
//            code = data.getString("code");
//            LogUtil.e(Utils.TAG, "js code parse : " + code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!StringUtil.isEmpty(code)) {
            updateInfo.setCode(code);
            updateInfo.setVersion(version);
            if (data.has(CREATETIME))
                updateInfo.setCreatetime(data.getString(CREATETIME));
        }
        return updateInfo;
    }
}
